package com.eedi.framework.config.service;

import com.eedi.framework.common.config.CfgConstant;
import com.eedi.framework.common.config.ConfigMap;
import com.eedi.framework.tenant.core.context.TenantContextHolder;
import com.eedi.framework.config.controller.sys.vo.app.HospitalAppCfgReq;
import com.eedi.framework.config.enums.HospitalAppTypeEnum;
import com.eedi.framework.config.enums.PayMerchantTypeEnum;
import com.eedi.framework.config.controller.sys.vo.app.HospitalAppCfgResp;
import com.eedi.framework.config.controller.sys.vo.paymerchant.PayMerchantCfgReq;
import com.eedi.framework.config.controller.sys.vo.paymerchant.PayMerchantCfgResp;
import com.eedi.framework.config.controller.sys.vo.supervision.SupervisionCfgReq;
import com.eedi.framework.config.controller.sys.vo.supervision.SupervisionCfgResp;
import com.eedi.framework.config.controller.sys.vo.sensitive.SensitiveCfgResp;
import com.eedi.framework.config.controller.sys.vo.sensitive.SensitiveCfgReq;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.eedi.framework.common.config.CfgConstant.*;


/**
 * @author zybourn
 * @version 1.0
 * @description 系统配置信息设置服务
 * @since 2024/3/13 19:15
 **/
//@Service
//@Validated
//@Slf4j
public class HospitalConfigServiceImpl implements  HospitalConfigService{

    @Resource
   private SysConfigValueService sysConfigValueService;
    @Override
    public void addOrUpdateHospitalAppConfig(HospitalAppCfgReq hospitalAppCfgReq) {
        ConfigMap toSave = new ConfigMap();
        //微信公众号
        if (HospitalAppTypeEnum.WECHAT_OFFICIAL_ACCOUNT.getCode().equals(hospitalAppCfgReq.getAppType())) {
            toSave.put(app_wechat_access_enable, Boolean.TRUE);
            toSave.put(app_wechat_access_type, hospitalAppCfgReq.getAppWechatAccessType());
            toSave.put(app_wechat_name, hospitalAppCfgReq.getAppWechatName());
            toSave.put(app_wechat_app_id, hospitalAppCfgReq.getAppWechatAppId());
            toSave.put(app_wechat_app_secret, hospitalAppCfgReq.getAppWechatAppSecret());
            sysConfigValueService.batchUpdate(TenantContextHolder.getTenantId(), toSave);
        }
    }

    @Override
    public void delHospitalAppConfig(Integer hospitalAppType) {
        if (HospitalAppTypeEnum.WECHAT_OFFICIAL_ACCOUNT.getCode().equals(hospitalAppType)) {
            sysConfigValueService.batchDel(TenantContextHolder.getTenantId(), CfgConstant.app_wechat_access_enable,
                    app_wechat_access_type,
                    app_wechat_name,
                    app_wechat_app_id,
                    app_wechat_app_secret);
        }
    }

    @Override
    public List<HospitalAppCfgResp> getHospitalAppConfig() {
        List<HospitalAppCfgResp> appConfigList = new ArrayList<>();
       Boolean appWechatAccessEnable = sysConfigValueService.get(CfgConstant.app_wechat_access_enable, TenantContextHolder.getTenantId());
        if (appWechatAccessEnable != null && appWechatAccessEnable) {
            HospitalAppCfgResp hospitalAppCfgResp = new HospitalAppCfgResp();
            ConfigMap configMap = sysConfigValueService.getList(TenantContextHolder.getTenantId(), app_wechat_access_type,
                    app_wechat_name,
                    app_wechat_app_id,
                    app_wechat_app_secret);
            hospitalAppCfgResp.setAppType(HospitalAppTypeEnum.WECHAT_OFFICIAL_ACCOUNT.getCode());
            hospitalAppCfgResp.setAppWechatAccessType(configMap.get(app_wechat_access_type));
            hospitalAppCfgResp.setAppWechatName(configMap.get(app_wechat_name));
            hospitalAppCfgResp.setAppWechatAppId(configMap.get(app_wechat_app_id));
            hospitalAppCfgResp.setAppWechatAppSecret(configMap.get(app_wechat_app_secret));
            appConfigList.add(hospitalAppCfgResp);
        }
        return appConfigList;
    }

    @Override
    public void addOrUpdatePayMerchantConfig(PayMerchantCfgReq payMerchantCfgReq) {
        ConfigMap toSave = new ConfigMap();
        //微信公众号
        if (PayMerchantTypeEnum.WECHAT.getCode().equals(payMerchantCfgReq.getPayMerchantType())) {
            toSave.put(pay_merchant_wechat_access_enable, Boolean.TRUE);
            toSave.put(pay_merchant_wechat_no, payMerchantCfgReq.getPayMerchantWechatNo());
            toSave.put(pay_merchant_wechat_name, payMerchantCfgReq.getPayMerchantWechatName());
            toSave.put(pay_merchant_wechat_api_v2_secret, payMerchantCfgReq.getPayMerchantWechatApiV2Secret());
            toSave.put(pay_merchant_wechat_api_certificate_url, payMerchantCfgReq.getPayMerchantWechatApiCertificateUrl());
            sysConfigValueService.batchUpdate(TenantContextHolder.getTenantId(), toSave);
        }
    }

    @Override
    public void delPayMerchantConfig(Integer payMerchantType) {
        if (PayMerchantTypeEnum.WECHAT.getCode().equals(payMerchantType)) {
            sysConfigValueService.batchDel(TenantContextHolder.getTenantId(), CfgConstant.pay_merchant_wechat_access_enable,
                    pay_merchant_wechat_no,
                    pay_merchant_wechat_name,
                    pay_merchant_wechat_api_v2_secret,
                    pay_merchant_wechat_api_certificate_url);
        }
    }

    @Override
    public List<PayMerchantCfgResp> getPayMerchantConfig() {
        List<PayMerchantCfgResp> payMerchantConfigList = new ArrayList<>();
        Boolean payWechatMerchantAccessEnable = sysConfigValueService.get(CfgConstant.pay_merchant_wechat_access_enable, TenantContextHolder.getTenantId());
        if (payWechatMerchantAccessEnable != null && payWechatMerchantAccessEnable) {
            PayMerchantCfgResp payMerchantCfgResp = new PayMerchantCfgResp();
            ConfigMap configMap = sysConfigValueService.getList(TenantContextHolder.getTenantId(), pay_merchant_wechat_no,
                    pay_merchant_wechat_name,
                    pay_merchant_wechat_api_v2_secret,
                    pay_merchant_wechat_api_certificate_url);
            payMerchantCfgResp.setPayMerchantType(PayMerchantTypeEnum.WECHAT.getCode());
            payMerchantCfgResp.setPayMerchantWechatNo(configMap.get(pay_merchant_wechat_no));
            payMerchantCfgResp.setPayMerchantWechatName(configMap.get(pay_merchant_wechat_name));
            payMerchantCfgResp.setPayMerchantWechatApiV2Secret(configMap.get(pay_merchant_wechat_api_v2_secret));
            payMerchantCfgResp.setPayMerchantWechatApiCertificateUrl(configMap.get(pay_merchant_wechat_api_certificate_url));
            payMerchantConfigList.add(payMerchantCfgResp);
        }
        return payMerchantConfigList;
    }

    @Override
    public void addOrUpdateSupervisionCfg(SupervisionCfgReq supervisionCfgReq) {
        ConfigMap toSave = new ConfigMap();
        toSave.put(mi_supervision_enable, supervisionCfgReq.getMiSupervisionEnable());
        toSave.put(mi_supervision_app_id, supervisionCfgReq.getMiSupervisionAppId());
        toSave.put(mi_supervision_app_key, supervisionCfgReq.getMiSupervisionAppKey());
        toSave.put(mi_supervision_url, supervisionCfgReq.getMiSupervisionUrl());
        toSave.put(health_supervision_enable, supervisionCfgReq.getHealthSupervisionEnable());
        toSave.put(health_supervision_app_id, supervisionCfgReq.getHealthSupervisionAppId());
        toSave.put(health_supervision_app_key, supervisionCfgReq.getHealthSupervisionAppKey());
        toSave.put(health_supervision_url, supervisionCfgReq.getHealthSupervisionUrl());
        sysConfigValueService.batchUpdate(TenantContextHolder.getTenantId(), toSave);
    }


    @Override
    public SupervisionCfgResp getSupervisionCfg() {
        SupervisionCfgResp supervisionCfgResp = new SupervisionCfgResp();
        ConfigMap configMap = sysConfigValueService.getList(TenantContextHolder.getTenantId(), mi_supervision_enable,
                mi_supervision_app_id,
                mi_supervision_app_key,
                mi_supervision_url,
                health_supervision_enable,
                health_supervision_app_id,
                health_supervision_app_key,
                health_supervision_url
        );
        supervisionCfgResp.setMiSupervisionEnable(configMap.get(mi_supervision_enable));
        supervisionCfgResp.setMiSupervisionAppId(configMap.get(mi_supervision_app_id));
        supervisionCfgResp.setMiSupervisionAppKey(configMap.get(mi_supervision_app_key));
        supervisionCfgResp.setMiSupervisionUrl(configMap.get(mi_supervision_url));
        supervisionCfgResp.setHealthSupervisionEnable(configMap.get(health_supervision_enable));
        supervisionCfgResp.setHealthSupervisionAppId(configMap.get(health_supervision_app_id));
        supervisionCfgResp.setHealthSupervisionAppKey(configMap.get(health_supervision_app_key));
        supervisionCfgResp.setHealthSupervisionUrl(configMap.get(health_supervision_url));
        return supervisionCfgResp;

    }

    @Override
    public void addOrUpdateSensitiveCfg(SensitiveCfgReq sensitiveCfgReq) {
        ConfigMap toSave = new ConfigMap();
        toSave.put(name_sensitive_enable, sensitiveCfgReq.getNameSensitiveEnable());
        toSave.put(id_card_sensitive_enable, sensitiveCfgReq.getIdCardSensitiveEnable());
        toSave.put(mobile_sensitive_enable, sensitiveCfgReq.getMobileSensitiveEnable());
        sysConfigValueService.batchUpdate(TenantContextHolder.getTenantId(), toSave);
    }

    @Override
    public SensitiveCfgResp getSensitiveCfg() {
        SensitiveCfgResp supervisionCfgRespVO =new SensitiveCfgResp();
        ConfigMap configMap = sysConfigValueService.getList(TenantContextHolder.getTenantId(), name_sensitive_enable,
                id_card_sensitive_enable,
                mobile_sensitive_enable
        );
        supervisionCfgRespVO.setNameSensitiveEnable(configMap.get(name_sensitive_enable));
        supervisionCfgRespVO.setIdCardSensitiveEnable(configMap.get(id_card_sensitive_enable));
        supervisionCfgRespVO.setMobileSensitiveEnable(configMap.get(mobile_sensitive_enable));
        return supervisionCfgRespVO;    }
}
